package com.huawei.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huawei.entity.Shangpinspu;
import com.huawei.service.SpuService;
import com.huawei.util.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName SpuController
 * @Description TODO
 * @Author 快乐的大胖
 * @Date 2021/11/30 17:05
 */
@Controller
@RequestMapping("/Spu")
public class SpuController {


    @Resource
    SpuService spuImpl;


    //查询
    @RequestMapping(value = "/chaxun", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String selectAll(
            @RequestParam(value = "spu_name", required = false) String spu_name,
            @RequestParam(value = "status", required = false) String status,
            @RequestParam(value = "kaishi", required = false) String kaishi,
            @RequestParam(value = "jieshu", required = false) String jieshu,
            @RequestParam("limit") Integer limit,
            @RequestParam("page") Integer page
    ) {
        Map<String, Object> pars = new HashMap<String, Object>();
        pars.put("spu_name", spu_name);
        pars.put("status", status);
        pars.put("kaishi", kaishi);
        pars.put("jieshu", jieshu);
        pars.put("offset", (page - 1) * limit);
        pars.put("limit", limit);

        List<Shangpinspu> list = spuImpl.selectAll(pars);
        int count = spuImpl.selectCount(pars);
        Result result = new Result(0, "查询成功", count, list);
        return JSON.toJSONString(result);
    }

    //修改


    @RequestMapping(value = "/xiugai", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String update(
            @RequestBody Shangpinspu shangpinspu

    ) {
        int i = spuImpl.update(shangpinspu);
         if (i == 1) {
                Result result=new Result(0, "修改成功", 1, i);
            return  JSON.toJSONString(result);
        } else {
            Result result=new Result(1, "修改失败", 1,null);
            return null;

        }
    }

    @RequestMapping(value = "/update1", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String update1(
            @RequestParam("spu_id") Integer spu_id,
            @RequestParam("status") String status
    ) {

        int i = spuImpl.update1(spu_id, status);
        if (i == 1) {
            Result result=new Result(0, "修改成功", 1, i);
            return  JSON.toJSONString(result);
        } else {
            Result result=new Result(1, "修改失败", 1,null);
            return null;

        }

    }

    //添加
    @RequestMapping(value = "insert", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String insert(
            @RequestParam Shangpinspu shangpinspu
    ) {
        int i = spuImpl.insert(shangpinspu);
        if (i == 1) {
            Result result=new Result(0, "添加成功", 1, i);
            return  JSON.toJSONString(result);
        } else {
            Result result=new Result(1, "添加失败", 1,null);
            return null;

        }


    }

    //删除
    @RequestMapping(value = "delete", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String detele(
            @RequestParam ("spu_id")  Integer spu_id
    ) {

        int i = spuImpl.delete(spu_id);
        if (i == 1) {
            Result result=new Result(0, "删除成功", 1, i);
            return  JSON.toJSONString(result);
        } else {
            Result result=new Result(1, "删除失败", 1,null);
            return null;

        }

    }

}





